<?php

namespace App\Http\Controllers\Api;

use App\model\Drug;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Lib\Utils\ArrayUtils;
use Lib\Utils\ModelUtils;

class DrugController extends Controller
{
    /**
     * @SWG\Post(
     *     path="/drug/get",
     *     summary="获取菜品列表",
     *     tags={"Dish"},
     *     @SWG\Parameter(name="page",in="formData",description="页码", required=true,type="string"),
     *     @SWG\Response(
     *         response=200,
     *         description="It's ok"
     *     ),
     *     @SWG\Response(
     *         response="default",
     *         description="unexpected error"
     *     )
     * )
     */
    public function get(){
        return Drug::paginate(15);

    }
    /**
     * @SWG\Post(
     *     path="/drug/del",
     *     summary="获取菜品列表",
     *     tags={"Dish"},
     *     @SWG\Parameter(name="dishid",in="formData",description="菜品id", required=true,type="array",
     *         @SWG\Items(type="string"),
     *         collectionFormat="multi"),
     *     @SWG\Response(
     *         response=200,
     *         description="It's ok"
     *     ),
     *     @SWG\Response(
     *         response="default",
     *         description="unexpected error"
     *     )
     * )
     */
    public function del(Request $request){
        
        Drug::destroy(ArrayUtils::ToArray($request->input("id")));
        return Drug::paginate(15);
    }
    /**
     * @SWG\Post(
     *     path="/drug/save",
     *     summary="获取菜品列表",
     *     tags={"Dish"},
     *     @SWG\Parameter(name="page",in="formData",description="页码", required=true,type="string"),
     *     @SWG\Parameter(name="normal_name",in="formData",description="页码", required=false,type="string"),
     *     @SWG\Parameter(name="offical_name",in="formData",description="页码", required=true,type="string"),
     *     @SWG\Response(
     *         response=200,
     *         description="It's ok"
     *     ),
     *     @SWG\Response(
     *         response="default",
     *         description="unexpected error"
     *     )
     * )
     */
    public function  save(Request $request){

        $form=$request->all();
        if(isset($form["id"])){
                $dish=Drug::find($form["id"]);

        }
        else{
           $dish=new Drug();

        }
        ModelUtils::requestToModel($request,$dish);
        $dish->save();
        return Drug::paginate(15);
    }
}
